package com.mikepenz.interactor.imp;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.mikepenz.bean.RequestBean;
import com.mikepenz.presenter.listener.RequestListener;
import com.mikepenz.utils.MethodCode;
import com.mikepenz.utils.MethodType;
import com.quickdv.helper.NetWorkImp;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lilongfei on 15/10/29.
 */
public class PayInteractorImp extends NetWorkImp {
    RequestListener<JSONObject> listener;

    public PayInteractorImp(Context c, RequestListener<JSONObject> listener) {
        super(c);
        this.listener = listener;
    }

    public void getOrder(String userId, String cardId, float money, String type, String currentDate) {
        RequestBean bean = new RequestBean();
        Map<String, Object> params = new HashMap<>();
        params.put("cardId", cardId);
        params.put("money", money);
        params.put("type", type);
        params.put("currentDate", currentDate);
        bean.setRequestParams(params);
        bean.setUserSession(userId);
        bean.setRequestCode(MethodType.GET_ORDER_CODE);
        addFastJsonQueue(Request.Method.POST, MethodType.GET_ORDER_CODE, null, (JSONObject) JSON.toJSON(bean));
        startRequest();
//        onRequestSuccess("tag", new JSONObject());
    }

    @Override
    protected void onRequestSuccess(Object tag, JSONObject result) {
        JSONObject response = result.getJSONObject(MethodCode.responseContent_key);
        if (response == null) {
           return;
        } else {
            int statue = response.getInteger(MethodCode.responseStatue);

            if (statue == (MethodCode.NetState.SUCCESS.getValue())) {

                if (tag.toString().equals(MethodType.GET_ORDER_CODE)) {
                    listener.Success(tag + "", response);
                }
            }else {
                listener.Error(tag + "", "获取订单号失败");
            }
        }
    }

    @Override
    protected void onRequestExcption(VolleyError error) {
        listener.Exception("", error);
    }
}
